import UIKit

class SliderView: UIViewController {
    
    private var slider = UISlider()
    private var valueLabel = UILabel()
    private var resetButton = UIButton(type: .system)
    
    private let minValue: Float = 0
    private let maxValue: Float = 100
    private let initialValue: Float = 50
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .systemBackground
        setupUI()
        setupConstraints()
    }
    
    private func setupUI() {
        // 创建 UISlider 并禁用自动调整约束
        slider = UISlider()
        slider.translatesAutoresizingMaskIntoConstraints = false // 关键修复
        slider.minimumValue = minValue
        slider.maximumValue = maxValue
        slider.value = initialValue
        slider.minimumTrackTintColor = .systemBlue
        slider.maximumTrackTintColor = .systemGray
        slider.thumbTintColor = .white
        slider.addTarget(self, action: #selector(sliderValueChanged(_:)), for: .valueChanged)
        view.addSubview(slider)
        
        // 创建值标签并禁用自动调整约束
        valueLabel = UILabel()
        valueLabel.translatesAutoresizingMaskIntoConstraints = false // 关键修复
        valueLabel.font = UIFont.systemFont(ofSize: 18)
        valueLabel.textAlignment = .center
        valueLabel.text = "当前值: \(Int(initialValue))"
        view.addSubview(valueLabel)
        
        // 创建重置按钮并禁用自动调整约束
        resetButton = UIButton(type: .system)
        resetButton.translatesAutoresizingMaskIntoConstraints = false // 关键修复
        resetButton.setTitle("重置", for: .normal)
        resetButton.backgroundColor = .systemBlue
        resetButton.tintColor = .white
        resetButton.layer.cornerRadius = 8
        resetButton.addTarget(self, action: #selector(resetSlider), for: .touchUpInside)
        view.addSubview(resetButton)
    }
    
    private func setupConstraints() {
        let margin: CGFloat = 20
        NSLayoutConstraint.activate([
            // Slider 约束
            slider.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: margin * 2),
            slider.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -margin * 2),
            slider.centerYAnchor.constraint(equalTo: view.centerYAnchor),
            
            // 值标签约束
            valueLabel.centerXAnchor.constraint(equalTo: view.centerXAnchor),
            valueLabel.bottomAnchor.constraint(equalTo: slider.topAnchor, constant: -margin), // 注意符号方向（topAnchor 在上方，所以用 -margin）
            
            // 重置按钮约束
            resetButton.centerXAnchor.constraint(equalTo: view.centerXAnchor),
            resetButton.topAnchor.constraint(equalTo: slider.bottomAnchor, constant: margin * 2), // 按钮在滑块下方，用 +margin
            resetButton.widthAnchor.constraint(equalToConstant: 120),
            resetButton.heightAnchor.constraint(equalToConstant: 44)
        ])
    }
    
    
    // MARK: - 交互逻辑
    @objc private func sliderValueChanged(_ sender: UISlider) {
        // 获取当前值并更新标签
        let currentValue = Int(sender.value)
        valueLabel.text = "当前值: \(currentValue)"
        
        // 可以在这里添加其他响应逻辑，例如调整音量、改变颜色等
        updateUIForValue(currentValue)
    }
    
    @objc private func resetSlider() {
        // 重置滑块到初始值
        slider.setValue(initialValue, animated: true)
        sliderValueChanged(slider)  // 触发更新标签
    }
    
    private func updateUIForValue(_ value: Int) {
        // 根据滑块值改变背景色（示例效果）
        let normalizedValue = CGFloat(value) / CGFloat(maxValue)
        let red: CGFloat = 0.8 - (normalizedValue * 0.6)
        let green: CGFloat = 0.4 + (normalizedValue * 0.5)
        view.backgroundColor = UIColor(red: red, green: green, blue: 0.9, alpha: 1.0)
    }
}
